Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core] Fix race condition for earliest snapshot #4930

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

yunfengzhou-hub
Copy link
Contributor

Purpose

Linked issue: close #3479

This PR fixes a racing bug about earliest snapshot. When thread A is trying to get the earliest snapshot of a table, while the snapshot is being expired by thread B, thread A might throw exception that it cannot get the snapshot information from the earliest snapshot id.

This PR fixes the bug by adding retrying logic to seek the second earliest snapshot if the first one cannot be found.

Note that, theoretically speaking, any snapshot (including the latest one) could encounter this issue, but the probability is quite small, so this PR only fixes the bug for invocations on the earliest snapshot.

Tests

Tests in SnapshotManagerTest are used to verify against the changes.

API and Format

This change does not affect API or storage format.

Documentation

This change does not introduce a new feature.

Long earliest;
if (startFromChangelog) {
Long earliestChangelog = earliestLongLivedChangelogId();
earliest = earliestChangelog == null ? earliestSnapshot : earliestChangelog;
earliest = earliestChangelog == null ? earliestSnapshotId : earliestChangelog;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

earliestChangelogId

return null;
}

Long latest = null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

latestSnapshotId

try {
earliestSnapshot = tryGetChangelogOrSnapshot(earliest);
break;
} catch (FileNotFoundException e) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a log here?

do {
try {
return tryGetSnapshot(snapshotId);
} catch (FileNotFoundException e) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a log here?

@@ -163,11 +163,28 @@ public Snapshot tryGetSnapshot(long snapshotId) throws FileNotFoundException {
return snapshot;
}

private @Nullable Snapshot tryGetEarliestSnapshotLaterThanOrEqualTo(
long snapshotId, long stopId) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stopSnapshotId?

Long earliestWatermark = null;
// find the first snapshot with watermark
if ((earliestWatermark = snapshot(earliest).watermark()) == null) {
if ((earliestWatermark = earliestSnapShot.watermark()) == null) {
while (earliest < latest) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there are some risks. Earliest should be re-assigned.

Maybe you should just optimize earliestSnapshot. And use this method in many places.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that some earliestSnapshot methods use binary search while others use linear, I extracted and optimized two kinds of paths. Please take a look.

@yunfengzhou-hub yunfengzhou-hub force-pushed the snapshot-earliest-race branch 2 times, most recently from b4ad65b to 86fb5b5 Compare February 12, 2025 07:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug] streaming read by from-timestamp may be occur exception(snapshot file not fund) sometimes
3 participants